var tpl = $("#myTemplate").html();
$.ajax({
    url:"/assets/script/index.hbs",
    type:"get",
    async:false,
    success:function(data){
        tpl=data;
    }
});


var template = Handlebars.compile(tpl);
Handlebars.registerHelper('if_eq', function(v1, v2, opts) {
    if(v1 == v2)
        return opts.fn(this);
    else
        return opts.inverse(this);
});

init();

function init(){
    var params;
    initEvent();
}
function initEvent(){
    params = {status:1};
    search();
    $('.condition-a').click(function(){
        params.status = $(this).data('p');
        search();
    });
    //点击样式切换
    $('.condition-a').click(function(){
        $('.condition-a').removeClass('con-active');
        $(this).addClass('con-active');
    });
}

function search(){
    $.ajax({
        url:"/project-center.json",
        type:"get",
        data:params,
        success:function(data){
            $('.number-project').html(data.data.totalCount);
            var html = template(data);
            $('.content-check').empty();
            $('.content-check').append(html);
        }
    });
}


///分页
var tpl1 = $("#myTemplate").html();
$.ajax({
    url:"/assets/script/pagination.hbs",
    type:"get",
    async:false,
    success:function(data){
        tpl1 = data;
    },
    error:function(err){

    }
});
var template1 = Handlebars.compile(tpl1);
$.ajax({
    url:'project-center.json',
    type:'get',
    dataType:'json',
    success:function(data){
        var array=[];
        for(var i=1;i<=data.data.totalPages;i++){
            array.push(i);
        }
        data.array=array;
        var html = template1(data);
        $('.pagina').append(html);
        $('.real a').click(function(){
            $('.pages').css('display','block');
        });
        var total = data.data.totalPages;
        $('.real a').data('total',total);
        $(document).click(function(){
            $('.pages').css('display','none');
        });
        $(".pages").click(function(event){
            event.stopPropagation();
        });
        $('.real a').click(function(event){
            event.stopPropagation();
        });
        //中间页数
        $('.page a').click(function(){
            $('.real a').html($(this).html());
            $('.pages').css('display','none');
            var current = $(this).data('p');
            $('.real a').data('p',current);
            $.ajax({
                url:'project-center.json',
                type:'get',
                data:{
                    currentPage:current
                },
                success:function(data){
                    var html = template(data);
                    $('.project-content').empty();
                    $('.project-content').append(html);
                },
                error:function(msg){
                }
            })
        });
        //上一页
        $('.prev').click(function(){
            var current = Number($('.real a').data('p'))-1;
            if(current<1){
                current = 1;
            }else{
                $.ajax({
                    url:'project-list.json',
                    type:'get',
                    data:{
                        currentPage:current
                    },
                    success:function(data){
                        var html = template(data);
                        $('.project-content').empty();
                        $('.project-content').append(html);
                    },
                    error:function(msg){
                    }
                })
            }

        });
        //下一页
        $('.next').click(function(){
            var current = Number($('.real a').data('p'))+1;
            var total = $('.real a').data('total');
            if(current>total){
                current = total;
            }else{
                $.ajax({
                    url:'project-list.json',
                    type:'get',
                    data:{
                        currentPage:current
                    },
                    success:function(data){
                        var html = template(data);
                        $('.project-content').empty();
                        $('.project-content').append(html);
                    },
                    error:function(msg){
                    }
                })
            }

        });
    },
    error:function(){

    }
});
